package com.parenting.controller;


import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.parenting.entities.OrderApprovalDO;
import com.parenting.model.IMPage;
import com.parenting.request.platform.SearchOrderApproval;
import com.parenting.response.OrderApprovalResponse;
import com.parenting.response.platform.ApprovalResponse;
import com.parenting.response.platform.ReasonsResponse;
import com.parenting.service.OrderApprovalService;
import com.parenting.util.base.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;

/**
 * <p>
 * 平台审核退款 前端控制器
 * </p>
 *
 * @author 烟台业达-王世玺
 * @since 2021-10-25
 */
@Api(tags = "平台退款审核通知")
@RestController
@RequestMapping("/approval")
public class OrderApprovalController {

    @Autowired
    private OrderApprovalService orderApprovalService;


    @ApiOperation(value = "平台退款审核列表", httpMethod = "POST")
    @PostMapping("/notice")
    public Result<IMPage<OrderApprovalResponse>> getOrderApprovalList(@RequestBody @Validated SearchOrderApproval searchOrderApproval){
        IMPage<OrderApprovalResponse> orderApprovalList = orderApprovalService.getOrderApprovalList(searchOrderApproval);
        return Result.buildSuccess(orderApprovalList);
    }


    @ApiOperation(value = "平台退款审详情", httpMethod = "GET")
    @GetMapping("/notice/{orderApprovalId}")
    public Result<ApprovalResponse> getOrderApproval(@PathVariable("orderApprovalId")
                                                         @NotNull(message = "退款通知ID不能为空")
                                                                 String orderApprovalId){
        ApprovalResponse orderApproval = orderApprovalService.getOrderApproval(orderApprovalId);
        return Result.buildSuccess(orderApproval);
    }

    @ApiOperation(value = "平台退款拒绝接口", httpMethod = "PUT")
    @PutMapping("/notice/reasons")
    public Result Reasons(@RequestBody ReasonsResponse reasonsResponse){
        LambdaUpdateWrapper<OrderApprovalDO>  wrapper =new LambdaUpdateWrapper<>();
//        @ApiModelProperty(value = "订单状态;1待审批、2通过、3拒绝")
        wrapper.set(OrderApprovalDO::getOrderState,3);
        wrapper.set(OrderApprovalDO::getReason,reasonsResponse.getReasons());
        wrapper.eq(OrderApprovalDO::getOrderApprovalId,reasonsResponse.getOrderApprovalId());
        orderApprovalService.update(wrapper);
        return  Result.buildSuccess();
    }


    @ApiOperation(value = "平台退款同意接口", httpMethod = "PUT")
    @PutMapping("/notice/agree/{orderApprovalId}")
    public Result agree(@PathVariable("orderApprovalId") String orderApprovalId){
        orderApprovalService.agree(orderApprovalId);
        return  Result.buildSuccess();
    }

}

